package co.edu.usbcali.sam.presentation.backingBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import co.edu.usbcali.sam.exceptions.ZMessManager;
import co.edu.usbcali.sam.modelo.Asignaturas;
import co.edu.usbcali.sam.modelo.Aula;
import co.edu.usbcali.sam.modelo.AulaAsig;
import co.edu.usbcali.sam.modelo.TipoIdentificacion;
import co.edu.usbcali.sam.modelo.dto.AulaAsigDTO;
import co.edu.usbcali.sam.presentation.businessDelegate.IBusinessDelegatorView;
import co.edu.usbcali.sam.utilities.FacesUtils;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * www.zathuracode.org
 *
 */
@ManagedBean
@ViewScoped
public class AulaAsigView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtDia;
    private InputText txtHora;
    private InputText txtIdAsignatura_Asignaturas;
    private InputText txtIdAula_Aula;
    private InputText txtIdAulaAsig;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private HtmlSelectOneMenu cmbAsignaturas;
    private HtmlSelectOneMenu cmbAulas;
    private List<AulaAsigDTO> data;
    private AulaAsigDTO selectedAulaAsig;
    private AulaAsig entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;
    private List<SelectItem> listAsignaturas = new ArrayList<SelectItem>();
    private String asignaturaSel;
    private List<SelectItem> listAula = new ArrayList<SelectItem>();
    private String aulaSel;
    List<Asignaturas> asignaturas= new ArrayList<>();
    List<Aula> aulas= new ArrayList<>();

    public AulaAsigView() {
        super();
    }
    
    @PostConstruct
    public void cargarDatos() {
    	
    	try {
    		asignaturas= businessDelegatorView.getAsignaturas();
			for(int i =0; i<asignaturas.size();i++)
	        {
				listAsignaturas.add(new SelectItem(asignaturas.get(i).getNombre()));
	        }
			aulas=businessDelegatorView.getAula();
			for(int i =0; i<aulas.size();i++)
	        {
				listAula.add(new SelectItem(aulas.get(i).getIdAula()));
	        }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AulaAsigDTO aulaAsigDTO = (AulaAsigDTO) e.getObject();

            if (txtDia == null) {
                txtDia = new InputText();
            }

            txtDia.setValue(aulaAsigDTO.getDia());

            if (txtHora == null) {
                txtHora = new InputText();
            }

            txtHora.setValue(aulaAsigDTO.getHora());

            if (txtIdAsignatura_Asignaturas == null) {
                txtIdAsignatura_Asignaturas = new InputText();
            }

            txtIdAsignatura_Asignaturas.setValue(aulaAsigDTO.getIdAsignatura_Asignaturas());

            if (txtIdAula_Aula == null) {
                txtIdAula_Aula = new InputText();
            }

            txtIdAula_Aula.setValue(aulaAsigDTO.getIdAula_Aula());

            if (txtIdAulaAsig == null) {
                txtIdAulaAsig = new InputText();
            }

            txtIdAulaAsig.setValue(aulaAsigDTO.getIdAulaAsig());

            Long idAulaAsig = FacesUtils.checkLong(txtIdAulaAsig);
            entity = businessDelegatorView.getAulaAsig(idAulaAsig);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedAulaAsig = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedAulaAsig = null;

        if (txtDia != null) {
            txtDia.setValue(null);
            txtDia.setDisabled(true);
        }

        if (txtHora != null) {
            txtHora.setValue(null);
            txtHora.setDisabled(true);
        }
        
        if (cmbAsignaturas != null) {
        	cmbAsignaturas.setValue(null);
        	cmbAsignaturas.setDisabled(true);
        }
        
        if (cmbAulas != null) {
        	cmbAulas.setValue(null);
        	cmbAulas.setDisabled(true);
        }

        /*if (txtIdAsignatura_Asignaturas != null) {
            txtIdAsignatura_Asignaturas.setValue(null);
            txtIdAsignatura_Asignaturas.setDisabled(true);
        }

        if (txtIdAula_Aula != null) {
            txtIdAula_Aula.setValue(null);
            txtIdAula_Aula.setDisabled(true);
        }*/

        if (txtIdAulaAsig != null) {
            txtIdAulaAsig.setValue(null);
            txtIdAulaAsig.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() throws Exception {
        try {
            Long idAulaAsig = FacesUtils.checkLong(txtIdAulaAsig);
            entity = (idAulaAsig != null)
                ? businessDelegatorView.getAulaAsig(idAulaAsig) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtDia.setDisabled(false);
            txtHora.setDisabled(false);
            //txtIdAsignatura_Asignaturas.setDisabled(false);
            //txtIdAula_Aula.setDisabled(false);
            txtIdAulaAsig.setDisabled(false);
            cmbAsignaturas.setDisabled(false);
            cmbAulas.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtDia.setValue(entity.getDia());
            txtDia.setDisabled(false);
            txtHora.setValue(entity.getHora());
            txtHora.setDisabled(false);
            //txtIdAsignatura_Asignaturas.setValue(entity.getAsignaturas()
              //                                         .getIdAsignatura());
            //txtIdAsignatura_Asignaturas.setDisabled(false);
            //txtIdAula_Aula.setValue(entity.getAula().getIdAula());
            //txtIdAula_Aula.setDisabled(false);
            txtIdAulaAsig.setValue(entity.getIdAulaAsig());
            txtIdAulaAsig.setDisabled(true);
            btnSave.setDisabled(false);
            
            List<Asignaturas> asignaturas=new ArrayList<>();
            asignaturas=businessDelegatorView.getAsignaturas();
            Asignaturas asign=new Asignaturas();
            
            for(int i=0;i<asignaturas.size();i++)
            {
            	if(asignaturas.get(i).getIdAsignatura().equals(entity.getAsignaturas().getIdAsignatura()))
            		asign=asignaturas.get(i);
            }
            
            cmbAsignaturas.setValue(asign.getNombre());
            cmbAsignaturas.setDisabled(false);
            
            cmbAulas.setValue(entity.getAula().getIdAula());
            cmbAulas.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedAulaAsig = (AulaAsigDTO) (evt.getComponent().getAttributes()
                                             .get("selectedAulaAsig"));
        txtDia.setValue(selectedAulaAsig.getDia());
        txtDia.setDisabled(false);
        txtHora.setValue(selectedAulaAsig.getHora());
        txtHora.setDisabled(false);
        txtIdAsignatura_Asignaturas.setValue(selectedAulaAsig.getIdAsignatura_Asignaturas());
        txtIdAsignatura_Asignaturas.setDisabled(false);
        txtIdAula_Aula.setValue(selectedAulaAsig.getIdAula_Aula());
        txtIdAula_Aula.setDisabled(false);
        txtIdAulaAsig.setValue(selectedAulaAsig.getIdAulaAsig());
        txtIdAulaAsig.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedAulaAsig == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new AulaAsig();

            Long idAulaAsig = FacesUtils.checkLong(txtIdAulaAsig);
            
            List<Asignaturas> asignaturas=new ArrayList<>();
            asignaturas=businessDelegatorView.getAsignaturas();
            Asignaturas asign=new Asignaturas();
            
            for(int i=0;i<asignaturas.size();i++)
            {
            	if(asignaturas.get(i).getNombre().equals(asignaturaSel))
            		asign=asignaturas.get(i);
            }
            
            Aula aula=businessDelegatorView.getAula(Long.parseLong(aulaSel));

            entity.setDia(FacesUtils.checkString(txtDia));
            entity.setHora(FacesUtils.checkString(txtHora));
           // entity.setIdAulaAsig(idAulaAsig);
            entity.setAsignaturas(asign);
            entity.setAula(aula);
            businessDelegatorView.saveAulaAsig(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idAulaAsig = new Long(selectedAulaAsig.getIdAulaAsig());
                entity = businessDelegatorView.getAulaAsig(idAulaAsig);
            }

            List<Asignaturas> asignaturas=new ArrayList<>();
            asignaturas=businessDelegatorView.getAsignaturas();
            Asignaturas asign=new Asignaturas();
            
            for(int i=0;i<asignaturas.size();i++)
            {
            	if(asignaturas.get(i).getNombre().equals(asignaturaSel))
            		asign=asignaturas.get(i);
            }
            
            Aula aula=businessDelegatorView.getAula(Long.parseLong(aulaSel));
            
            entity.setDia(FacesUtils.checkString(txtDia));
            entity.setHora(FacesUtils.checkString(txtHora));
            entity.setAsignaturas(asign);
            entity.setAula(aula);
            businessDelegatorView.updateAulaAsig(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedAulaAsig = (AulaAsigDTO) (evt.getComponent().getAttributes()
                                                 .get("selectedAulaAsig"));

            Long idAulaAsig = new Long(selectedAulaAsig.getIdAulaAsig());
            entity = businessDelegatorView.getAulaAsig(idAulaAsig);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idAulaAsig = FacesUtils.checkLong(txtIdAulaAsig);
            entity = businessDelegatorView.getAulaAsig(idAulaAsig);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteAulaAsig(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedAulaAsig = (AulaAsigDTO) (evt.getComponent().getAttributes()
                                                 .get("selectedAulaAsig"));

            Long idAulaAsig = new Long(selectedAulaAsig.getIdAulaAsig());
            entity = businessDelegatorView.getAulaAsig(idAulaAsig);
            businessDelegatorView.deleteAulaAsig(entity);
            data.remove(selectedAulaAsig);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String dia, String hora, Long idAulaAsig,
        Long idAsignatura_Asignaturas, Long idAula_Aula)
        throws Exception {
        try {
            entity.setDia(FacesUtils.checkString(dia));
            entity.setHora(FacesUtils.checkString(hora));
            businessDelegatorView.updateAulaAsig(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AulaAsigView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtDia() {
        return txtDia;
    }

    public void setTxtDia(InputText txtDia) {
        this.txtDia = txtDia;
    }

    public InputText getTxtHora() {
        return txtHora;
    }

    public void setTxtHora(InputText txtHora) {
        this.txtHora = txtHora;
    }

    public InputText getTxtIdAsignatura_Asignaturas() {
        return txtIdAsignatura_Asignaturas;
    }

    public void setTxtIdAsignatura_Asignaturas(
        InputText txtIdAsignatura_Asignaturas) {
        this.txtIdAsignatura_Asignaturas = txtIdAsignatura_Asignaturas;
    }

    public InputText getTxtIdAula_Aula() {
        return txtIdAula_Aula;
    }

    public void setTxtIdAula_Aula(InputText txtIdAula_Aula) {
        this.txtIdAula_Aula = txtIdAula_Aula;
    }

    public InputText getTxtIdAulaAsig() {
        return txtIdAulaAsig;
    }

    public void setTxtIdAulaAsig(InputText txtIdAulaAsig) {
        this.txtIdAulaAsig = txtIdAulaAsig;
    }

    public List<AulaAsigDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataAulaAsig();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<AulaAsigDTO> aulaAsigDTO) {
        this.data = aulaAsigDTO;
    }

    public AulaAsigDTO getSelectedAulaAsig() {
        return selectedAulaAsig;
    }

    public void setSelectedAulaAsig(AulaAsigDTO aulaAsig) {
        this.selectedAulaAsig = aulaAsig;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

	public HtmlSelectOneMenu getCmbAsignaturas() {
		return cmbAsignaturas;
	}

	public void setCmbAsignaturas(HtmlSelectOneMenu cmbAsignaturas) {
		this.cmbAsignaturas = cmbAsignaturas;
	}

	public HtmlSelectOneMenu getCmbAulas() {
		return cmbAulas;
	}

	public void setCmbAulas(HtmlSelectOneMenu cmbAulas) {
		this.cmbAulas = cmbAulas;
	}

	public List<SelectItem> getListAsignaturas() {
		return listAsignaturas;
	}

	public void setListAsignaturas(List<SelectItem> listAsignaturas) {
		this.listAsignaturas = listAsignaturas;
	}

	public String getAsignaturaSel() {
		return asignaturaSel;
	}

	public void setAsignaturaSel(String asignaturaSel) {
		this.asignaturaSel = asignaturaSel;
	}

	public List<SelectItem> getListAula() {
		return listAula;
	}

	public void setListAula(List<SelectItem> listAula) {
		this.listAula = listAula;
	}

	public String getAulaSel() {
		return aulaSel;
	}

	public void setAulaSel(String aulaSel) {
		this.aulaSel = aulaSel;
	}
    
    
}
